package serviceImp;

import java.util.List;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import pojo.Genre;
import pojo.Movie;
import dao.GenreHome;
import dao.MovieHome;
import pojo.Video;
import dao.VideoHome;
import service.IMovieService;

public class MovieServiceImp implements IMovieService {

    private static final Logger _logger = Logger
	    .getLogger(MovieServiceImp.class);

    private MovieHome movieDAO;
    
    private VideoHome videoDAO;
    
    private GenreHome genreDAO;

    public MovieServiceImp() {

    }

    public MovieServiceImp(MovieHome movieDAO, VideoHome videoDAO, GenreHome genreDAO) {
	super();
	this.movieDAO = movieDAO;
	this.videoDAO = videoDAO;
	this.genreDAO = genreDAO;
    }

  

    public void setMovieDAO(MovieHome movieDAO) {
        this.movieDAO = movieDAO;
    }

    public void setVideoDAO(VideoHome videoDAO) {
        this.videoDAO = videoDAO;
    }

    public void setGenreDAO(GenreHome genreDAO) {
        this.genreDAO = genreDAO;
    }

    public Movie getMoviebyId(int movieid) {

	Movie movie = movieDAO.findById(movieid);

	if (movie == null) {
	    _logger.log(Level.INFO, "Movie not found");
	    return null;
	}

	return movie;
    }

    public List<Movie> getAllMovies() {

	List<Movie> movies = movieDAO.findByExample(new Movie());

	if (movies != null) {
	    _logger.log(Level.DEBUG,
		    String.format("%d movie found", movies.size()));
	}

	return movies;
    }

    public List<Movie> getMoviesByCatagory(int cat_id) {

	Genre genre = genreDAO.findById(cat_id);
	
	Movie example = new Movie();
	example.setMGenre(genre.getGenreId());
	List<Movie> movies = movieDAO.findByExample(example);
	if (movies != null) {
	    _logger.log(Level.DEBUG,
		    String.format("%d movie found", movies.size()));
	}

	return movies;

    }

    public List<Movie> searchMovie(Movie movie) {

	List<Movie> movies = movieDAO.findByExample(movie);
	if (movies != null) {
	    _logger.log(Level.DEBUG,
		    String.format("%d movie found", movies.size()));
	}

	return movies;

    }

    public boolean addMovie(Movie newMovie) {

	try {
	    movieDAO.attachDirty(newMovie);
	    return true;
	}catch (RuntimeException e) {
	    _logger.log(org.apache.log4j.Level.ERROR, "add new movie fail");
	    return false;
	}
    }

    public List<Video> getMovieAllVideos(int movie_id) {
	Video video = new Video();
	video.setVMovie(movie_id);
	List<Video> videos = videoDAO.findByExample(video);
	
	return videos;
    }

    public boolean hasVideo(int movie_id) {
	Video video = new Video();
	video.setVMovie(movie_id);
	List<Video> videos = videoDAO.findByExample(video);
	
	if (videos.size()>0) {
	    return true;
	}
	return false;
    }

    public List<Genre> getALLMovieGenre() {
	 Genre parentExample = new Genre();
	 parentExample.setGenres(null);
	 
	 List<Genre> parentGenres = genreDAO.findByExample(parentExample);
	 
	 if (parentGenres.size()<=0) {
	    return null; 
	}
	return parentGenres;
    }

    public List getALLMoiveYear() {
	
	List years =movieDAO.findAllYear();
	 
	return years;
    }

    public List<Movie> searchMoviesByKeyword(String keyword) {
	List<Movie> result = movieDAO.findByKeyword(keyword);
	return result;
    }
    
    public void deletemovie(int movieId) {
	
	try {
	    Movie movie = getMoviebyId(movieId);
		movieDAO.delete(movie);
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return;
	
    }
    
    public void updateMovie(Movie movie) {
	
	try {
	    movieDAO.attachDirty(movie);
	    //movieDAO.persist(movie);
	} catch (Exception e) {
	    // TODO: handle exception
	}
	return;
    }

}
